package com.zzxx.controller;

import com.github.pagehelper.PageInfo;
import com.zzxx.exceptions.StatusCodeException;
import com.zzxx.pojo.Product;
import com.zzxx.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
//@CrossOrigin
@RequestMapping("product")
public class ProductController {

	@Autowired
	private ProductService productService;

	@RequestMapping("find")
	public PageInfo<Product> findAll(Integer pageNum, Integer pageSize){
		pageNum = pageNum==null? 1:pageNum;
		pageSize = pageSize==null? 5:pageSize;
		PageInfo<Product> pageInfo = productService.findAll(pageNum, pageSize);
		return pageInfo;
	}

	@RequestMapping(value = "find/{id}", method = RequestMethod.GET)
	public Product findById(@PathVariable("id") String id) {
		Product product = productService.findById(id);
		return product;
	}

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public boolean add(@RequestBody Product product){
		return productService.add(product);
	}

	@RequestMapping(value = "delete", method = RequestMethod.DELETE)
	public boolean delete(@RequestBody Map<String,Object> map)  {
		List<String> list = (List<String>) map.get("delBox");
		return productService.deleteById(list);
	}


	@RequestMapping(value = "update",method = RequestMethod.PUT)
	public boolean update(@RequestBody Map<String,Product> map){
		Product product = map.get("product");
		return productService.update(product);
	}

	@RequestMapping(value = "update/state",method = RequestMethod.PUT)
	public boolean updateState(@RequestBody Map<String,Object> map){
		List<String> list = (List<String>) map.get("selectedBox");
		Integer state = (Integer) map.get("state");
		return productService.updateStatus(list,state);
	}

}
